<?php
namespace common\models\member;

use Yii;
use yii\base\Model;
use yii\bootstrap\Html;
use common\models\member\Member;
use common\models\member\Level;
use common\enums\ActiveEnum;
use common\helpers\TreeHelper;

/**
 * 推荐关系图
 */
class TuiJianGuanXiTu extends Model
{
    const SELECT_FILTER = 'id,pid,username,realname,promo_amount,current_level,team_amount,my_amount';
    public $username; // 会员ID
    public $dai = 4; // 查看几层

    public $pid;
    public $p_username;
    public $topMember; // 如果设置了顶点会员，就只能看这个会员团队下的会员

    public $levels = [2,4,6];
    public $isAdmin;

    public function rules()
    {
        return [
            ['username', 'exist', 'targetClass' => '\common\models\member\Member', 'message' => '会员不存在'],
            ['dai', 'integer'],
            ['username', 'loadInfo'],
        ];
    }

    public function attributeLabels()
    {
        return [
            'dai' => '查看代数',
            'username' => '会员编号',
        ];
    }

    public function loadInfo()
    {
        $member = Member::findOne(['username'=>$this->username]);
        if ($member) {
            $pMember = Member::findOne($member->pid);
            if ($pMember) {
                $this->pid = $pMember->id;
                $this->p_username = $pMember->username;
            }
            if ($this->topMember) {
                if ($this->topMember->id == $member->id) {
                    $this->pid = 0;
                    $this->p_username = '';
                } else {
                    // 判断是否在团队内
                    $treeStr = $member->tree;
                    $tree = str_replace(TreeHelper::PREFIX, '', $treeStr);
                    $ids = explode(TreeHelper::SEPARATOR, $tree);
                    if (!in_array($this->topMember->id, $ids)) {
                        return $this->addError('username', '会员不存在或没权限');
                    }
                }

            }
        } else {
            $this->addError('username', '会员不存在'); // 有点多余~~
        }
    }

    public function getJsonData()
    {
        return json_encode($this->getData());
    }

    public function getData()
    {
        $member = Member::find()
                        ->where(['username'=>$this->username])
                        ->select(self::SELECT_FILTER)
                        ->asArray()
                        ->one();
        $res = [];
        $res[] = $this->changeData($member);
        $this->getChildren($res, $member);
        return $res;
    }


    public function getChildren(&$arr, $father, $dai=1)
    {
        if ($dai >= $this->dai) {
            return true; // 超出代数了
        }
        $list = Member::find()
                        ->where(['pid'=>$father['id']])
                        ->select(self::SELECT_FILTER)
                        ->asArray()
                        ->all();
        foreach ($list as $key => $val) {
            $arr[] = $this->changeData($val, $father['id']);
            $this->getChildren($arr, $val, $dai+1);
        }

    }

    public function changeData($member, $parentId=null)
    {
        $res = $member;
        if ($parentId) {
            $res['parent'] = $parentId;
        }
        $res['key'] = $member['id'];
        $res['usernameAndNickname'] = $res['username'] . ' ' . $res['realname'];
        $res['levelName'] = Level::getLevelName($res['current_level']);
        // $res['xfLevelName'] = $res['xf_level'] == -1 ? '无' : Member::getXfLevelMap($res['xf_level']);
        // $res['lock_amount'] = \common\models\member\Account::getLockMoneyByMemberId($res['id']);
        $msgArr = [
            "[级别:{$res['levelName']}]",
            // "[团队级别:{$res['xfLevelName']}]",
            "[推荐人数:{$res['promo_amount']}]",
            "[个人累计消费:{$res['my_amount']}]",
            "[团队业绩:{$res['team_amount']}]",
            // "[冻结兑换券:{$res['lock_amount']}]",
        ];
        // if ($this->isAdmin) {
        // }
        $res['color'] = '#009bcf';
        $res['msg'] = implode(' ', $msgArr);
        return $res;
    }

}